<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\PastureOrder;
use app\common\model\PastureOrderProduct;
use app\common\tool\account\Account;
use app\common\facade\Refund;

class PastureOrderRefund
{
    public function handle($info)
    {
        try {
            $account = $info->getData('pay_way');

            if (in_array($account, ['wechat', 'alipay'])) {
                $out_trade_no = PastureOrder::where('id', $info->order_id)->value('out_trade_no');
                //在线支付方式待处理
                if('wechat' == $account){
                    $notifyUrl = 'https://' . $_SERVER['HTTP_HOST'] . '/time/callback/pastureWechatRefund';
                    //设置参数
                    $refund = Refund::setDriver('WeChat')
                        ->setOutTradeNo($out_trade_no)
                        ->setOutRefundNo($info->sn)
                        ->setRefundFee((int)($info->total*100))
                        ->setTotalFee((int)($info->total*100))
                        ->setNotifyUrl($notifyUrl)
                        ->setMerchantType('pasture')
                        ->create($info->refund_method);
                }else{
                    $refund = Refund::setDriver('AliPay')
                        ->setOutTradeNo($out_trade_no)
                        ->setRefundFee($info->total)
                        ->create('App');
                }

                if($refund['code'] == 1){
                    $info->response = json_encode($refund['result'], JSON_UNESCAPED_UNICODE);
                    self::operations($info);
                }else{
                    return ['code'=>-1, 'msg'=>'退款失败', 'data'=>$refund];
                }

            } else {
                $accountAll = [$account];
                $amountAll = [round($info->total - $info->rabbit_coupon - $info->ranch_bonus - $info->ranch_points, 2)];
                if ($info->rabbit_coupon > 0) {
                    $accountAll[] = 'rabbit_coupon';
                    $amountAll[] = $info->rabbit_coupon;
                }
                if ($info->ranch_bonus > 0) {
                    $accountAll[] = 'ranch_bonus';
                    $amountAll[] = $info->ranch_bonus;
                }
                if ($info->ranch_points > 0) {
                    $accountAll[] = 'ranch_points';
                    $amountAll[] = $info->ranch_points;
                }

                Account::getInstance()->basics($info->uid, $accountAll, $amountAll, '云牧场订单退款', '+')->setOrder($info->order_id)->createNormal();

                self::operations($info);
            }

            return ['code'=>1, 'msg'=>'退款成功'];
        } catch (\Exception $e) {
            return ['code'=>-1, 'msg'=>$e->getMessage()];
        }
    }

    private static function operations($info)
    {
        $info->status = 4;
        $info->refund_time = time();
        $info->save();

        PastureOrderProduct::whereIn('id', $info->op_ids)->save(['return_status' => 5]);

        if(PastureOrderProduct::whereNotIn('return_status', [0,5])->find()){
            $is_return = -1;
        }elseif (!PastureOrderProduct::where('return_status', '<>', 5)->find()){
            $is_return = 2;
        }else{
            $is_return = 1;
        }

        PastureOrder::where('id', $info->order_id)->save(['is_return'=>$is_return]);

        return true;
    }

}